/*
 * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Nam Nguyen
 */

package redoak.php.bz2;

import redoak.php.bz2.Bz2Module;
import redoak.php.bz2.impl.CBZip2OutputStream;

import java.io.IOException;
import java.io.OutputStream;

import com.caucho.quercus.QuercusModuleException;
import com.caucho.quercus.lib.file.AbstractBinaryOutput;

/**
 * As opposed to java's GZIPOutputStream, this class allows for more control on
 * what is written to the underlying OutputStream. 
 *
 * @see java.util.zip.GZIPOutputStream
 */
public class Bz2FileOutputStream extends AbstractBinaryOutput {
  private OutputStream _os;
  private CBZip2OutputStream _out;

  private long _inputSize;

  /**
   * @param out
   * @param compressionLevel
   * @param strategy Deflate compression strategy
   * @param encodingMode FORCE_GZIP to write gzwrite compatible output;
   *    FORCE_DEFLATE to write gzip header and zlib header, but do not
   *    write crc32 trailer
   */
  public Bz2FileOutputStream(OutputStream os,
			  int compressionLevel)
    throws IOException
  {
    _os = os;
    _os.write(new byte[]{'B','Z'});
    _os.flush();
    _out = new CBZip2OutputStream(_os, compressionLevel);
  }

  /**
   * @param out
   */
  public Bz2FileOutputStream(OutputStream os)
    throws IOException
  {
    this(os,500);
  }

  /**
   * Writes a byte.
   *
   * @param input
   */
  public void write(int v)
    throws IOException
  {
    _out.write(v);

    _inputSize++;
    
  }

  /**
   * @param input
   * @param offset
   * @param length
   */
  public void write(byte[] buffer, int offset, int length)
  throws IOException
  {
    _out.write(buffer, offset, length);
    
    _inputSize += length;
    
  }

  public void flush()throws IOException
  {
	  _out.flush();
  }

  public void closeWrite()
  {
    close();
  }
  
  public void close()
  {
    try {
    	_out.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * Returns false always for a write stream.
   */
  public boolean isEOF()
  {
    return false;
  }

  /**
   * Tells the position in the gzip stream
   */
  public long getPosition()
  {
    return _inputSize;
  }

  /**
   * Sets the position.
   */
  public boolean setPosition(long offset)
  {
    if (offset < _inputSize)
      return false;

    offset -= _inputSize;
    byte[] buffer = new byte[128];

    try {
      while (offset > 0) {
        int sublen = (int)Math.min(offset, buffer.length);
        write(buffer, 0, sublen);
        offset -= sublen;
      }

      return true;
    } catch (IOException e) {
      throw new QuercusModuleException(e);
    }
  }

  public String toString()
  {
    return "Bz2OutputStream[]";
  }
}
